package com.we.flink.dcapiuserlabel.tag;

import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.RichFlatMapFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.util.Collector;

import com.alibaba.fastjson.JSONObject;
import com.we.flink.dcapiuserlabel.table.TableDcAuthRecord;

public class TagDcAuthRecord {
    public long uid;
    public int dc_submit_audit_time;

    public TagDcAuthRecord(long uid, int dc_submit_audit_time) {
        this.uid = uid;
        this.dc_submit_audit_time = dc_submit_audit_time;
    }

    public static TagDcAuthRecord init(long uid) {
        return new TagDcAuthRecord(uid, 0);
    }

    public static SingleOutputStreamOperator<String> getFlatMapStream(
            StreamTableEnvironment tableEnv, StreamExecutionEnvironment env) {
        Table tableRes = tableEnv.sqlQuery(TableDcAuthRecord.QUERYSQL);
        DataStream<Tuple2<Boolean, TableDcAuthRecord>> inputStream =
                tableEnv.toRetractStream(tableRes, TableDcAuthRecord.class);

        SingleOutputStreamOperator<String> out =
                inputStream
                        .filter(
                                new FilterFunction<Tuple2<Boolean, TableDcAuthRecord>>() {
                                    @Override
                                    public boolean filter(Tuple2<Boolean, TableDcAuthRecord> value)
                                            throws Exception {
                                        return value.f0;
                                    }
                                })
                        .keyBy(
                                new KeySelector<Tuple2<Boolean, TableDcAuthRecord>, Long>() {
                                    @Override
                                    public Long getKey(Tuple2<Boolean, TableDcAuthRecord> value)
                                            throws Exception {
                                        return value.f1.getUid();
                                    }
                                })
                        .flatMap(
                                new RichFlatMapFunction<
                                        Tuple2<Boolean, TableDcAuthRecord>, String>() {
                                    ValueState<TagDcAuthRecord> dcAuthRecordTagVState;

                                    @Override
                                    public void open(Configuration parameters) throws Exception {
                                        dcAuthRecordTagVState =
                                                getRuntimeContext()
                                                        .getState(
                                                                new ValueStateDescriptor<
                                                                        TagDcAuthRecord>(
                                                                        "DcAuthRecordTagVState",
                                                                        TagDcAuthRecord.class));
                                    }

                                    @Override
                                    public void flatMap(
                                            Tuple2<Boolean, TableDcAuthRecord> value,
                                            Collector<String> out)
                                            throws Exception {
                                        int time =
                                                (int) (value.f1.getCreate_time().getTime() / 1000);
                                        long uid = value.f1.getUid();

                                        TagDcAuthRecord tmpDcAuthRecord =
                                                dcAuthRecordTagVState.value();
                                        TagDcAuthRecord curDcAuthRecord =
                                                tmpDcAuthRecord == null
                                                        ? TagDcAuthRecord.init(uid)
                                                        : tmpDcAuthRecord;

                                        if (curDcAuthRecord.dc_submit_audit_time < time) {
                                            JSONObject jsonObject = new JSONObject();
                                            jsonObject.put("uid", uid);
                                            jsonObject.put("dc_submit_audit_time", time);
                                            out.collect(jsonObject.toString());

                                            curDcAuthRecord.dc_submit_audit_time = time;
                                            dcAuthRecordTagVState.update(curDcAuthRecord);
                                        }
                                    }
                                });

        return out;
    }

    public long getUid() {
        return uid;
    }

    public int getDc_submit_audit_time() {
        return dc_submit_audit_time;
    }
}
